package com.hjx.other;

/**
 * @author houjinxin
 * @version 创建时间：2019/10/18.
 */
public class NumToChinese {


    /**
     * 数字转人民币大写
     */

    public static String transfer(double amount){

        String[] token1s = {
                "拾",
                "佰",
                "仟",
                "万",
                "拾万",
                "佰万",
                "仟万",
                "亿",
                "拾亿",
                "佰亿",
                "仟亿",
        };
        String[] token2s = {
                "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖",
        };

        String[] token3s = {
                "角",
                "分",
        };

        String result = "";
        String amountStr = String.valueOf(amount);

        if(amountStr.endsWith(".0")){

//        } else {
            char[] chars = amountStr.split("\\.")[0].toCharArray();
            int tokenIndex = 0;
            for (int i = chars.length - 1; i >= 0; i--) {
                //当前位的值
                int diff = chars[i] - '0';
                if (i == chars.length - 1){
                    result += token2s[diff] + "圆";
                } else {
                    result = result.indexOf(token2s[diff]) == 0
                            ? result
                            : (diff == 0 ? token2s[diff] : token2s[diff] + token1s[tokenIndex]) + result;

                    tokenIndex++;
                }

            }
            result += "整";
        } else {
            String[] array = amountStr.split("\\.");
            char[] chars = array[0].toCharArray();
            int tokenIndex = 0;
            for (int i = chars.length - 1; i >= 0; i--) {
                //当前位的值
                int diff = chars[i] - '0';
                if (i == chars.length - 1){
                    result += token2s[diff] + "圆";
                } else {

                    result = result.indexOf(token2s[diff]) == 0
                            ? result
                            : (diff == 0 ? token2s[diff] : token2s[diff] + token1s[tokenIndex]) + result;
                    tokenIndex++;
                }

            }

            char[] chars1 = array[1].toCharArray();
            int tokenIndex1 = 0;
            for (int i = 0; i < chars1.length; i++) {
                //当前位的值
                int diff = chars1[i] - '0';
                    result += token2s[diff] + token3s[tokenIndex1++];
            }
        }
        return result;


    }


    public String int2String(int num){
        switch (num) {
            case 0 :
                return "零";
            case 1 :
                return "壹";
            case 2 :
                return "贰";
            case 3 :
                return "叁";
            case 4 :
                return "肆";
            case 5 :
                return "伍";
            case 6 :
                return "陆";
            case 7 :
                return "柒";
            case 8 :
                return "捌";
            case 9 :
                return "玖";

        }
        return "";
    }

    public static void main(String[] args) {
//        System.out.println(transfer(123.31));
        System.out.println(transfer(10023.31));
    }
}
